package com.xk857.simpleeffect.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.reflect.TypeToken;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.AddTodoActivity;
import com.xk857.simpleeffect.adapter.ListNoBusinessAdapter;
import com.xk857.simpleeffect.adapter.ListYesBusinessAdapter;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.vo.TodayBusinessVO;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.ListViewUtil;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.progress.CircleProgressView;

import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Date;
import java.util.function.Consumer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;

public class TodayFragment extends Fragment {
    private View view;
    private CircleProgressView progressViewCircleMain;
    private TextView dateTextView;
    private TextView weekTextView;
    private ConstraintLayout emptyLayout;
    private ListView noBusinessListView;
    private ListView yesBusinessListView;
    private ConstraintLayout yesMainConstraint;
    private RefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_fragment_today, container, false);
        progressViewCircleMain = view.findViewById(R.id.progressView_circle_main);
        initView();
        return view;
    }

    private void initView() {
        dateTextView = view.findViewById(R.id.dateTextView);
        weekTextView = view.findViewById(R.id.weekTextView);
        emptyLayout = view.findViewById(R.id.empty_layout);
        yesMainConstraint = view.findViewById(R.id.yes_main_constraint);
        noBusinessListView = view.findViewById(R.id.no_business_list_view);
        yesBusinessListView = view.findViewById(R.id.yes_business_list_view);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        initDate();

        FloatingActionButton addBtn = view.findViewById(R.id.add_btn);
        addBtn.setOnClickListener(v -> {
            if (getActivity() != null) {
                getActivity().startActivity(new Intent(getActivity(), AddTodoActivity.class));
            }
        });
        initRefresh();
    }

    private void initRefresh() {
        refreshLayout.setEnableHeaderTranslationContent(true);
        //设置 Header 为 贝塞尔雷达 样式
        if (getActivity() != null) {
//            refreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setPrimaryColorId(R.color.main).setEnableHorizontalDrag(true));
            refreshLayout.setEnableHeaderTranslationContent(false);
            refreshLayout.setRefreshHeader(new MaterialHeader(getActivity()).setColorSchemeResources(R.color.main));
        }
        // 下拉刷新
        refreshLayout.setOnRefreshListener(mLayout -> getBusinessList(false));
    }

    private void initDate() {
        dateTextView.setText(DateUtil.format(new Date(), "MM月dd日"));
        weekTextView.setText(getWeek(new Date()));
    }

    @Override
    public void onResume() {
        super.onResume();
        progressViewCircleMain.setProgress(0);
        getBusinessList(true);
    }

    private void getBusinessList(boolean isLoading) {
        Type type = new TypeToken<Result<TodayBusinessVO>>() {}.getType();
        OkHttpUtils.getInstance().get(getActivity(), RequestEnum.TODAY_BUSINESS_LIST, type, isLoading, (Consumer<TodayBusinessVO>) vo -> {
            refreshLayout.finishRefresh();
            if (getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    if (vo.getNoBusinessList().size() > 0) {
                        noBusinessListView.setAdapter(new ListNoBusinessAdapter(getActivity(), vo.getNoBusinessList(), aBoolean -> {
                            if (getActivity() != null) {
                                getActivity().runOnUiThread(() -> getBusinessList(false));
                            }
                        }));
                        noBusinessListView.setVisibility(View.VISIBLE);
                        emptyLayout.setVisibility(View.GONE);
                        ListViewUtil.setListViewHeight(noBusinessListView);
                    } else {
                        noBusinessListView.setVisibility(View.GONE);
                        emptyLayout.setVisibility(View.VISIBLE);
                    }

                    if (vo.getYesBusinessList().size() > 0) {
                        yesBusinessListView.setAdapter(new ListYesBusinessAdapter(getActivity(), vo.getYesBusinessList(), aBoolean -> {
                            if (getActivity() != null) {
                                getActivity().runOnUiThread(() -> getBusinessList(false));
                            }
                        }));
                        yesMainConstraint.setVisibility(View.VISIBLE);
                        ListViewUtil.setListViewHeight(yesBusinessListView);
                    } else {
                        yesMainConstraint.setVisibility(View.GONE);
                    }

                    if (getActivity() != null) {
                        if (vo.getNoBusinessList().size() == 0 && vo.getYesBusinessList().size() == 0) {
                            progressViewCircleMain.setProgress(0);
                        } else {
                            double a = NumberUtil.div(vo.getYesBusinessList().size(), (vo.getYesBusinessList().size() + vo.getNoBusinessList().size()));
                            progressViewCircleMain.setProgress((int) (a * 100));
                        }
                    }
                });
            }
        });
    }

    //根据日期取得星期几
    public static String getWeek(Date date) {
        String[] weeks = {"周末", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }
}
